﻿using SelfManagement.Mvc.Result;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace SelfManagement.Mvc
{
    public abstract class BaseController: Controller
    {
        #region ActionResult 

        public JsonResult JsonAllowGet(object data)
        {
            return Json(data, JsonRequestBehavior.AllowGet);
        }

        public AjaxFormResult AjaxFormResult(bool success, string message = null, bool reloadPage = false, string redirectUrl = null, object model = null)
        {
            if (success)
            {
                return new AjaxFormResult().Success(message).ReloadPage(reloadPage).RedirectTo(redirectUrl).WithModel(model);

            }
            else
            {
                return new AjaxFormResult().Failed(message).ReloadPage(reloadPage).RedirectTo(redirectUrl).WithModel(model);
            }
        }

        #endregion
        /// <summary>
        /// 获取文件实际地址
        /// </summary>
        /// <param name="fileName">文件名</param>
        /// <param name="fileCategory">文件分类</param>
        /// <returns>文件实际地址</returns>
        protected virtual string GetPhysicalPath(string fileName, string filePath)
        {
            //if (changeToFileName.IsNotNullAndNotWhiteSpace())
            //{
            //    fileName = changeToFileName + Path.GetExtension(fileName);
            //}
            return Path.Combine(HttpContext.Server.MapPath(filePath),
                               Path.GetFileName(fileName));
        }

        /// <summary>
        /// 获取文件相对地址
        /// </summary>
        /// <param name="fileName">文件名</param>
        /// <param name="fileCategory">文件分类</param>
        /// <returns>文件相对地址</returns>
        protected virtual string GetFileRelativePath(string fileName, string filePath)
        {
            return Path.Combine(filePath, Path.GetFileName(fileName));
        }
    }
}
